package com.jhe.hexed;

public class JHexEditorSelection
{
	public int cursor;
	private int selectionStart;
	private int selectionEnd;
	private boolean isInSelection;

	public JHexEditorSelection()
	{
		clear();
	}
	
	public void clear()
	{
		selectionStart = selectionEnd = -1;
		isInSelection = false;
	}

	public int getSelectionStart() 
	{
		return (selectionStart == selectionEnd ? cursor : selectionStart);
	}

	public int getSelectionEnd()
	{
		return (selectionStart == selectionEnd ? cursor : selectionEnd);
	}
	
	public int getSelectionFirst()
	{
		return (selectionStart < selectionEnd ? selectionStart : selectionEnd); 
	}

	public int getSelectionLast()
	{
		return (selectionStart > selectionEnd ? selectionStart : selectionEnd); 
	}

	public void selectRange(int start, int end)
	{
		selectionStart = start;
		selectionEnd = end;
	}

	public boolean isInSelection()
	{
		return isInSelection;
	}
	
	public void startSelect(int pos_from)
	{
		selectRange(pos_from, pos_from);
		isInSelection = true;
	}
	
	public void updateSelection(int pos_to)
	{
		selectionEnd = pos_to;
	}
	
	public void endSelect(int pos_to)
	{
    	cursor = selectionStart;
    	selectionEnd = pos_to;
        if (selectionEnd == selectionStart)
        {
        	selectionStart = selectionEnd = -1;
        }
        else if (selectionStart > selectionEnd)
        {
        	selectRange(selectionEnd, selectionStart);
        }
        isInSelection = false;
	}
}
